"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DDL-table
Case Name   : subquery中查询同一张表的tablesample system repeatable
Create At   : 2023/04/03
Owner       : @kyeleze
Description :
    1.建表并插入数据
    2.subquery中查询同一张表的tablesample system repeatable,对比结果
Expect      :
    1.成功
    2.查询结果一致
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Table(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.table = 't_ddl_table_0331'

    def test_table(self):
        text = '-----step1:建表并插入数据 expect:成功-----'
        self.logger.info(text)
        sql_cmd = f"drop table if exists {self.table};" \
            f"create table {self.table}(id serial primary key,title text);" \
            f"insert into {self.table}(title) select 'Record #' || i from " \
            f"generate_series(1, 1000000) i;"
        self.logger.info(sql_cmd)
        msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(msg)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, msg, text +
                      '执行失败')
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, text +
                      '执行失败')
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败' + text)

        text = '-----step2:subquery中查询同一张表的tablesample system ' \
               'repeatable, 对比结果 expect:查询结果一致-----'
        self.logger.info(text)
        sql_cmd = f"select ctid,* from {self.table} tablesample " \
            f"system(0.1) repeatable (200) limit 1;"
        self.logger.info(sql_cmd)
        result1 = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(result1)
        sql_cmd = f"(select ctid,* from {self.table} tablesample " \
            f"system(0.1) repeatable (200) limit 1) union (select " \
            f"ctid,* from {self.table} tablesample system(0.1) " \
            f"repeatable (200) limit 1);"
        self.logger.info(sql_cmd)
        result2 = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(result2)
        result1_split = str(result1.splitlines()[-2]).strip()
        result2_split = str(result2.splitlines()[-2]).strip()
        self.assertEqual(result1_split, result2_split, '执行失败' + text)

    def tearDown(self):
        text = '-----清理环境 expect:成功-----'
        self.logger.info(text)
        clean_cmd = f"drop table {self.table};"
        self.logger.info(clean_cmd)
        clean_msg = self.sh_primy.execut_db_sql(clean_cmd)
        self.logger.info(clean_msg)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, clean_msg,
                      '执行失败' + text)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
